﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: FeatureBase.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2013-01-25 3:25 PM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Text;

namespace BOL.Models.Features
{
    /// <summary>
    /// Represents a specific annotation of important sequence features.
    /// </summary>
    public abstract class FeatureBase : IFeature
    {
        #region Public properties

        public FeatureKey Key { get; private set;  }

        public IOffset Parent { get; set; }

        public IRange<int> Location { get; set; }

        public IDictionary<string, dynamic> Qualifiers { get; set; }

        #endregion

        #region Constructors

        protected FeatureBase(FeatureKey key, IOffset parent, IRange<int> location)
        {
            Key = key;
            Parent = parent;
            Location = location;
            Qualifiers = new Dictionary<string, dynamic>();
        }

        #endregion

        #region Object overriden

        public override string ToString()
        {
            var sb = new StringBuilder();

            sb.AppendFormat("FEATURES             Location/Qualifiers\n");
            sb.AppendFormat("     {0,-15} {1}\n", Key, Location);
            foreach (var q in Qualifiers)
                sb.Append(GetQualiferString(q));
            
            return sb.ToString();
        }

        private static string GetQualiferString(KeyValuePair<string, dynamic> qualifer)
        {
            string value = qualifer.Value is int ? 
                String.Format("/{0}={1}", qualifer.Key, qualifer.Value) : 
                String.Format("/{0}=\"{1}\"", qualifer.Key, qualifer.Value);
            var sb = new StringBuilder();
            
            while (value.Length > 58)
            {
                sb.Append("                     ");
                sb.Append(value.Substring(0, 58));
                sb.AppendLine();
                value = value.Substring(58);
            }

            sb.Append("                     ");
            sb.Append(value);
            sb.AppendLine();

            return sb.ToString();
        }

        #endregion
    }
}
